package com.cxhome.cxinfox.cxmenu;

import java.util.List;

import com.jfinal.plugin.activerecord.Page;
import com.traffic.managent.annotation.ControllerBind;
import com.traffic.managent.base.CxBaseController;
import com.traffic.managent.model.system.Menu;
import com.traffic.managent.model.system.Operator;
import com.traffic.managent.util.SQLUtils;
import com.traffic.managent.util.UtilsUtils;

/**
 * 部门
 * 
 */
@ControllerBind(controllerKey = "/system/cxmenu")
public class MenuController extends CxBaseController {

	private static final String path = "/pages/system/cxmenu/cxmenu_";

	public void index() {
		list();
	}

	public void list() {
		Menu model = getModelByAttr(Menu.class);

		Page<Menu> page = MenuService.service.paginate(model, getPaginator(), new StringBuilder(""));
		List<Menu> menulist = Menu.dao.findByWhere(" where 1=1");

		setAttr("page", page);
		setAttr("attr", model);
		setAttr("cxmenus", menulist);
		render(path + "list.html");
	}

	
//	public void list() {
//		Menu model = getModelByAttr(Menu.class);
//
//		SQLUtils sql = new SQLUtils(" from cx_menu t "
//				+ " left join cx_menu p on t.parentmenuids = p.ids  left join cx_systems s on t.systemsids = s.ids ");
//		if (model.getAttrValues().length != 0) {
//			sql.setAlias("t");
//			sql.append(" where 1=1 ");
//			// 查询条件
//			sql.whereLike("names_zhcn", model.getStr("names_zhcn"));
//		}
//
//		Page<Menu> page = Menu.dao.paginate(getPaginator(), "select t.*,p.names_zhcn as parentName,s.names as sysnames", //
//				sql.toString().toString());
//
//		// 下拉框
//		setAttr("page", page);
//		setAttr("attr", model);
//
//		List<Menu> departments = Menu.dao.find("select * from cx_menu  ");
//		setAttr("cxmenus", departments);
//
//		render(path + "list.html");
//	}

	/**
	 * 跳转新增页面
	 */
	public void add() {
		render(path + "add.html");
	}

	/**
	 * 查看详情
	 */
	public void view() {
		Menu model = Menu.dao.findById(getPara());
		setAttr("model", model);
		render(path + "view.html");
	}

	/**
	 * 删除数据
	 */
	public void delete() {
		Menu.dao.deleteById(getPara());
		list();
	}

	/**
	 * 跳转编辑页面
	 */
	public void edit() {
		Menu model = Menu.dao.findById(getPara());
		setAttr("model", model);
		render(path + "edit.html");
	}

	/**
	 * 保存功能
	 */
	public void save() {
		Menu model = getModel(Menu.class);
		String ids = getPara();
		if (ids == null || "".equals(ids)) {
			model.put("ids", UtilsUtils.getRandomId(30));
			model.save();
		} else {
			model.update();
		}
		renderMessage("保存成功");
	}

	/**
	 * 编辑保存
	 */
	public void saveEdit() {
		Menu model = getModel(Menu.class);
		MenuService.service.saveEdit(model);
		renderMessage("保存成功");
	}

	/**
	 * 跳转授权功能页面
	 */
	public void setOperator() {
		String menuids = getPara();
		List<Operator> list = Operator.dao.findByWhere(" where 1=1 ");
		String operatorids = OperatorService.service.getAllOperators(menuids);// 获取所有功能

		setAttr("ids", menuids);
		setAttr("operatorids", operatorids);
		setAttr("list", list);// 根结点
		render(path + "operator.html");
	}

	/**
	 * 授权功能保存
	 */
	public void operator_save() {
		String menuids = getPara("ids");
		String operatorids = getPara("operatorids");
		MenuService.service.operator_save(menuids, operatorids);
		renderMessage("保存成功");
	}

}
